﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Activity
{
    public static class Log
    {
        /// <summary>
        /// 日志类型
        /// </summary>
        public enum LogType
        {
            /// <summary>
            /// 一般性日志
            /// </summary>
            LOG,
            /// <summary>
            /// 比较特别的日志
            /// </summary>
            INFO,
            /// <summary>
            /// 警告：可能会发生错误的地方
            /// </summary>
            WARMING,
            /// <summary>
            /// 错误：一般性错，例如：捕获到异常，或者判断到异常（比如：if(变量==null)）
            /// </summary>
            ERROR,
            /// <summary>
            /// 严重错误：导致操作无法正常进行（比如保存数据失败）
            /// </summary>
            FAIL,
            /// <summary>
            /// 严重错误：导致系统崩溃的
            /// </summary>
            CRASH = 99
        }
        /// <summary>
        /// 输出日志
        /// </summary>
        /// <param name="strMemo">日志内容</param>
        /// <param name="tag">标签，用于检索日志时方便进行过滤</param>
        /// <param name="type">类型</param>
        public static void WriteLog(string strMemo, string tag = "Normal", LogType type = LogType.LOG)
        {
            //#if DEBUG
            const long maxFileLength = 1024 * 100;
            var path = System.Web.HttpContext.Current.Server.MapPath("~/App_Data/Log");
            if (System.IO.Directory.Exists(path) == false)
            {
                System.IO.Directory.CreateDirectory(path);
            }
            var filename = System.IO.Path.Combine(path, DateTime.Now.ToString("yyyy-MM-dd") + ".txt");
            var index = 1;
            while (System.IO.File.Exists(filename))
            {
                var file = new System.IO.FileInfo(filename);
                if (file.Length > maxFileLength)
                {
                    filename = System.IO.Path.Combine(path, DateTime.Now.ToString("yyyy-MM-dd") + "_" + index + ".txt");
                    index++;
                }
                else
                {
                    break;
                }
            }
            System.IO.StreamWriter sr = null;
            try
            {
                sr = !System.IO.File.Exists(filename) ? System.IO.File.CreateText(filename) : System.IO.File.AppendText(filename);
                sr.WriteLine("----------------------------------------------------------");
                sr.WriteLine("{0}：{2}:{1},{1:fffff}", type, DateTime.Now, tag);
                sr.WriteLine("----------------------------------------------------------");
                sr.WriteLine(strMemo);
                sr.WriteLine("=END=LOG==================================================");
            }
            catch
            {
                // ignored
            }
            finally
            {
                if (sr != null)
                    sr.Close();
            }
            //#endif
        }
        /// <summary>
        /// 输出日志
        /// </summary>
        /// <param name="str">日志内容</param>
        /// <param name="tag">标签，用于检索日志时方便进行过滤</param>
        /// <param name="type">类型</param>
        /// <param name="args"></param>
        public static void WriteLog(string str, string tag = "Normal", LogType type = LogType.LOG, params object[] args)
        {
            WriteLog(args.Length > 0 ? string.Format(str, args) : str, tag, type);
        }
        /// <summary>
        /// 输出错误日志（Type=LogType.ERROR）
        /// </summary>
        /// <param name="str">日志内容</param>
        /// <param name="tag">标签，用于检索日志时方便进行过滤</param>
        /// <param name="args"></param>
        public static void Error(string str, string tag = "Normal", params object[] args)
        {
            WriteLog(args.Length > 0 ? string.Format(str, args) : str, tag, LogType.ERROR);
        }
        public static void Error(Exception ex, string tag = "Normal")
        {
            WriteLog(ex.Message + Environment.NewLine + ex.Source + Environment.NewLine + ex.StackTrace, tag, LogType.ERROR);
        }
        /// <summary>
        /// 输出错误日志（Type=LogType.CRASH）
        /// </summary>
        /// <param name="str">日志内容</param>
        /// <param name="tag">标签，用于检索日志时方便进行过滤</param>
        /// <param name="args"></param>
        public static void Crash(string str, string tag = "Normal", params object[] args)
        {
            WriteLog(args.Length > 0 ? string.Format(str, args) : str, tag, LogType.CRASH);
        }
    }
}
